<?php


namespace app\admin\controller;
use app\common\model\Visitor as ModelVisitor;
use app\common\model\Visitordata;
use think\exception\DbException;

class Visitor
{
    public function selectAll(){
        $visitor = new ModelVisitor();
        try {
            return $visitor
                ->order("v_record desc")
                ->limit(1000)
                ->select();
        } catch (DbException $e) {
        }
    }

    public function count_it($data){
        $number = 0;
        foreach($data as $value){
            $number += $value['v_number'];
        }
        return $number;
    }

    public function selectData(){
        $visitor_data = new Visitordata();
        $visitor = new ModelVisitor();
        $data  = $visitor->all();
        $time_day = date("Y-m-d");
        $time_month = date("Y-m");
        $time_year = date("Y");
        $number_1_6 =
            sizeof(
                $visitor
                ->where('v_time_a', '>', '00:00:00')
                ->where('v_time_a', '<', '06:00:00')
                ->all()
            );
        $number_6_12 = sizeof(
            $visitor
                ->where('v_time_a', '>', '06:00:00')
                ->where('v_time_a', '<', '12:00:00')
                ->all()
        );
        $number_12_18 = sizeof(
            $visitor
                ->where('v_time_a', '>', '12:00:00')
                ->where('v_time_a', '<', '18:00:00')
                ->all()
        );
        $number_18_24 = sizeof(
            $visitor
                ->where('v_time_a', '>', '18:00:00')
                ->where('v_time_a', '<', '24:00:00')
                ->all()
        );

        $number_day = $this->count_it(
            $visitor
                ->where('v_record', '>', $time_day.'00:00:00')
                ->all()
        );
        $number_month =  $this->count_it(
            $visitor
                ->where('v_record', '>', $time_month.'-01 00:00:00')
                ->all()
        );
        $number_year =  $this->count_it(
            $visitor
                ->where('v_record', '>', $time_year.'-01-01 00:00:00')
                ->all()
        );

        $_data = array(
            "number_ip"   => sizeof($data),
            "number_all"  => $visitor_data->where('id','1')->find()['number_all'],
            "number_day"  => $number_day,
            "number_month"=> $number_month,
            "number_year" => $number_year,

            "number_percentage_1_6"  => round(($number_1_6/sizeof($data)),4),
            "number_percentage_6_12" =>  round(($number_6_12/sizeof($data)),4),
            "number_percentage_12_18" =>  round(($number_12_18/sizeof($data)),4),
            "number_percentage_18_24" =>  round(($number_18_24/sizeof($data)),4),
         );
        return $_data;
    }
}